library(devtools) library(flexdashboard) library(lubridate) library(dplyr) library(ggplot2) library(ggstance) library(ggalt) library(viridisLite)
ggplot(data = fatal_police_shootings_data, aes(y = race)) +
geom_bar(aes(fill = ..count..)) +
theme_minimal(base_size = 13) +
theme(legend.position = "none") +
scale_x_continuous(expand=c(0,0)) +
scale_fill_gradient(low = "royalblue3", high = "navyblue") +
labs(y = NULL, x = "Number of deaths")
stateinfo <- fatal_police_shootings_data %>% group_by(state) %>% dplyr::summarise(n = n()) %>%
dplyr::arrange(desc(n)) %>% top_n(15) %>%
mutate(state = factor(state, levels = rev(unique(state))))
Selecting by n
ggplot(stateinfo, aes(x = n, y = state)) +
geom_bar(stat="identity", aes(fill = n)) +
geom_text(aes(x = 17, y = state, label=as.character(state)), color="white", size=4) +
labs(y = NULL, x = "Number of deaths") +
scale_fill_gradient(low = "grey",
high = "orange",space = "rgb",
na.value = "grey50", guide = "colourbar") +
theme_minimal(base_size = 13) +
theme(axis.text.y=element_blank()) +
theme(legend.position = "none") +
scale_x_continuous(expand=c(0,0))
Warning: Non Lab interpolation is deprecated
levels(fatal_police_shootings_data$gender) <- c("Female", "Male")
levels(fatal_police_shootings_data$race) <- c("Unknown", "Asian", "Black", "Hispanic", "Other", "White")
ggplot(data = knownraced, aes(y = race)) +
geom_bar(aes(fill = ..count..)) +
geom_vline(xintercept = 2500, linetype = 2, colour = "grey20") +
geom_text(x = 2500, y = 4, label = "majority of\nvictims",
hjust = 0, size = 11 * 0.8 / .pt, colour = "grey20") +
scale_x_continuous(expand = expansion(mult = c(0, 0.1))) +
scale_y_discrete(limits = rev) +
theme_minimal(base_size = 13) +
theme(legend.position = "none") +
scale_x_continuous(expand=c(0,0)) +
scale_colour_manual(breaks = c("W", "NA", "O", "B", "A", "H","N"),
values = c("NavyBlue", "darkred", "grey","brown", "yellow","darkgreen" , "darkred")) +
labs(y = NULL, x = "Total of Shootings")
Scale for 'x' is already present. Adding another scale for 'x', which will replace the
existing scale.
ggplot(data = fatal_police_shootings_data, aes(x = age)) +
geom_density(fill="#69b3a2", color="#e9ecef",adjust=1.5, alpha=0.4) +
theme_minimal(base_size = 13) +
theme(legend.position = "none") +
scale_x_continuous(expand=c(0,0)) +
scale_fill_gradient(low = "white", high = "navyblue") +
labs(x = "Age at death", y = "Density")
Warning: Removed 384 rows containing non-finite values
(stat_density).
armedinfo <- fatal_police_shootings_data %>% group_by(armed) %>% dplyr::summarise(n = n()) %>%
arrange(desc(n)) %>% top_n(10) %>%
mutate(armed = factor(armed, levels = rev(unique(armed))))
Selecting by n
ggplot(data = armedinfo, aes(x = n, y = armed)) +
geom_bar(stat="identity", aes(fill = n)) +
theme_minimal(base_size = 13) +
theme(legend.position = "none") +
scale_x_continuous(expand=c(0,0)) +
scale_fill_gradient(low = "royalblue3", high = "navyblue") +
labs(y = NULL, x = "Number of deaths")
genderinfo <- fatal_police_shootings_data %>% group_by(gender) %>% dplyr::summarise(n = n())
ggplot(data = genderinfo, aes(x = n, y = gender)) +
geom_bar(stat="identity", aes(fill = n)) +
theme_minimal(base_size = 13) +
theme(legend.position = "none") +
scale_x_continuous(expand=c(0,0)) +
scale_fill_gradient(low = "white", high = "darkgreen") +
labs(y = NULL, x = "Gender")
mybreaks <- c(0.02, 0.04, 0.08, 1, 7)
data <- fatal_police_shootings_data %>% group_by(fatal_police_shootings_data$state) %>% dplyr::summarise(n = n()) %>%
dplyr::arrange(desc(n))
fatal_police_shootings_data %>%
ggplot() +
geom_polygon(data = fatal_police_shootings_data, aes(x=fatal_police_shootings_data$longitude, y = fatal_police_shootings_data$latitude), fill="grey", alpha=0.3) +
geom_point( aes(x=fatal_police_shootings_data$longitude, y=fatal_police_shootings_data$latitude , size = after_stat(n)), shape=20, stroke=FALSE) +
scale_size_continuous(name="Shootings Total", range=c(1,1100), breaks=mybreaks) +
scale_alpha_continuous(name="Shootings Total", range=c(0.1, .9), breaks=mybreaks) +
scale_color_viridis(option="magma", trans="log", breaks=mybreaks, name="Shootings Total" ) +
theme_void() + ylim(50,59) + coord_map() +
guides( colour = guide_legend()) +
ggtitle("Shootings Distribution accros the US") +
theme(
legend.position = c(0.85, 0.8),
text = element_text(color = "#22211d"),
plot.background = element_rect(fill = "#f5f5f2", color = NA),
panel.background = element_rect(fill = "#f5f5f2", color = NA),
legend.background = element_rect(fill = "#f5f5f2", color = NA),
plot.title = element_text(size= 16, hjust=0.1, color = "#4e4d47", margin = margin(b = -0.1, t = 0.4, l = 2, unit = "cm")),
)
Error: Aesthetics must be valid computed stats. Problematic aesthetic(s): size = after_stat(n).
Did you map your stat in the wrong layer?
Run `rlang::last_error()` to see where the error occurred.
ggplot(data = fatal_police_shootings_data, aes(y = manner_of_death)) +
geom_bar(aes(fill = ..count..)) +
theme_minimal(base_size = 13) +
theme(legend.position = "none") +
scale_x_continuous(expand=c(0,0)) +
scale_fill_gradient(low = "grey", high = "darkred") +
labs(y = NULL, x = "Number of deaths")
us_cont <- fatal_police_shootings_data[fatal_police_shootings_data$state]
Error: Can't subset columns that don't exist.
x Columns `WA`, `OR`, `KS`, `CA`, `CO`, etc. don't exist.
Run `rlang::last_error()` to see where the error occurred.
yearinfo <- fatal_police_shootings_data %>% group_by(year) %>% dplyr::summarise(n = n())
yearinfo %>%
ggplot(aes(x = year, y = n)) +
geom_line( color="#69b3a2") +
geom_point(shape=21, color="black", fill="#69b3a2", size=6) +theme_minimal(base_size = 13)+
ggtitle("Rate of shootings remains steady")
fatal_police_shootings_data <- fatal_police_shootings_data %>%
mutate(year = lubridate::year(date),
month = lubridate::month(date),
day = lubridate::day(date))
yearly <- fatal_police_shootings_data %>% group_by(year , month) %>% dplyr::summarise(n = n())
`summarise()` has grouped output by 'year'. You can override using the `.groups` argument.
yearly %>%
ggplot(aes(x= month , y = n , group = year , color = year))+
geom_line(color="#69b3a2", size=1, alpha=0.9)+
scale_color_viridis(discrete = TRUE)+
ggtitle("Rate of Shootings remains steady (2015-2021)")+
theme_minimal()+
scale_colour_manual(name = "Year",
values = c("green3", "orange", "blue", "red", "grey")+
ylab("Total Shootings")
Error: Incomplete expression: yearly %>%
ggplot(aes(x= month , y = n , group = year , color = year))+
geom_line(color="#69b3a2", size=1, alpha=0.9)+
scale_color_viridis(discrete = TRUE)+
ggtitle("Rate of Shootings remains steady (2015-2021)")+
theme_minimal()+
scale_colour_manual(name = "Year",
values = c("green3", "orange", "blue", "red", "grey")+
ylab("Total Shootings")
geom_point( data=aqi_map, aes(x=long, y=lat,color=mean_pollution,size=mean_pollution))
Error in fortify(data) : object 'aqi_map' not found
fatal_police_shootings_data%>%
mutate(fatal_police_shootings_data, region =
ifelse(state=="CA"|state=="AZ"|state=="NM"|state=="CO"|state=="ID"|state=="OR"|
state=="WA"|state=="AK"|state=="HI", "West",
ifelse(state=="TX"|state=="OK"|state=="AR"|state=="LA"|state=="MS"|state=="AL"|
state=="TN"|state=="FL"|state=="GA"|state=="NC"|state=="SC"|state=="VA"|
state=="KY"|state=="MD"|state=="WV"|state=="DC","South",
ifelse(state=="KS"|state=="MO"|state=="IL"|state=="IN"|state=="OH"
|state=="IA"|state=="NE"|state=="SD"|state=="ND"|state=="MN"|
state=="WI"|state=="MI","Midwest", "Northeast"))))
fatal_police_shootings_data %>%
summary(subset(fatal_police_shootings_data, region == "Midwest"))
id name date manner_of_death armed
Min. : 3 Length:7001 Min. :2015-01-02 Length:7001 Length:7001
1st Qu.:1970 Class :character 1st Qu.:2016-10-12 Class :character Class :character
Median :3883 Mode :character Median :2018-07-05 Mode :character Mode :character
Mean :3871 Mean :2018-07-16
3rd Qu.:5771 3rd Qu.:2020-04-25
Max. :7645 Max. :2021-12-31
age gender race city state
Min. : 6.00 Length:7001 Length:7001 Length:7001 Length:7001
1st Qu.:27.00 Class :character Class :character Class :character Class :character
Median :35.00 Mode :character Mode :character Mode :character Mode :character
Mean :37.13
3rd Qu.:45.00
Max. :92.00
NA's :364
signs_of_mental_illness threat_level flee body_camera
Mode :logical Length:7001 Length:7001 Mode :logical
FALSE:5451 Class :character Class :character FALSE:6025
TRUE :1550 Mode :character Mode :character TRUE :976
longitude latitude is_geocoding_exact year month
Min. :-160.01 Min. :19.50 Mode :logical Min. :2015 Min. : 1.000
1st Qu.:-112.07 1st Qu.:33.48 FALSE:18 1st Qu.:2016 1st Qu.: 3.000
Median : -94.23 Median :36.10 TRUE :6983 Median :2018 Median : 6.000
Mean : -97.11 Mean :36.66 Mean :2018 Mean : 6.439
3rd Qu.: -83.12 3rd Qu.:40.01 3rd Qu.:2020 3rd Qu.:10.000
Max. : -67.87 Max. :71.30 Max. :2021 Max. :12.000
NA's :575 NA's :575
day
Min. : 1.00
1st Qu.: 8.00
Median :16.00
Mean :15.62
3rd Qu.:23.00
Max. :31.00
blacks= fatal_police_shootings_data[fatal_police_shootings_data$race=="B",]
Warning message:
In makeContext(x) : reached elapsed time limit
whites= fatal_police_shootings_data[fatal_police_shootings_data$race=="W",]
hisLats= fatal_police_shootings_data[fatal_police_shootings_data$race=="H",]
summary(blacks)
id name date manner_of_death
Min. : 17 Length:2665 Min. :2015-01-06 Length:2665
1st Qu.:1726 Class :character 1st Qu.:2016-07-11 Class :character
Median :3595 Mode :character Median :2018-04-05 Mode :character
Mean :3586 Mean :2018-04-12
3rd Qu.:5350 3rd Qu.:2019-12-19
Max. :7645 Max. :2021-12-30
NA's :1082 NA's :1082
armed age gender race
Length:2665 Min. :13.00 Length:2665 Length:2665
Class :character 1st Qu.:24.00 Class :character Class :character
Mode :character Median :31.00 Mode :character Mode :character
Mean :32.73
3rd Qu.:39.00
Max. :88.00
NA's :1113
city state signs_of_mental_illness
Length:2665 Length:2665 Mode :logical
Class :character Class :character FALSE:1337
Mode :character Mode :character TRUE :246
NA's :1082
threat_level flee body_camera longitude
Length:2665 Length:2665 Mode :logical Min. :-157.85
Class :character Class :character FALSE:1278 1st Qu.: -95.49
Mode :character Mode :character TRUE :305 Median : -87.21
NA's :1082 Mean : -90.76
3rd Qu.: -80.61
Max. : -70.28
NA's :1170
latitude is_geocoding_exact year month
Min. :20.89 Mode :logical Min. :2015 Min. : 1.000
1st Qu.:32.95 FALSE:2 1st Qu.:2016 1st Qu.: 3.000
Median :36.18 TRUE :1581 Median :2018 Median : 6.000
Mean :36.29 NA's :1082 Mean :2018 Mean : 6.244
3rd Qu.:39.96 3rd Qu.:2019 3rd Qu.: 9.000
Max. :61.58 Max. :2021 Max. :12.000
NA's :1170 NA's :1082 NA's :1082
day
Min. : 1.00
1st Qu.: 8.00
Median :15.00
Mean :15.51
3rd Qu.:23.00
Max. :31.00
NA's :1082
summary(whites)
id name date manner_of_death
Min. : 4 Length:4087 Min. :2015-01-02 Length:4087
1st Qu.:1696 Class :character 1st Qu.:2016-07-03 Class :character
Median :3438 Mode :character Median :2018-02-17 Mode :character
Mean :3488 Mean :2018-03-09
3rd Qu.:5298 3rd Qu.:2019-11-26
Max. :7641 Max. :2021-12-26
NA's :1082 NA's :1082
armed age gender race
Length:4087 Min. : 6 Length:4087 Length:4087
Class :character 1st Qu.:30 Class :character Class :character
Mode :character Median :38 Mode :character Mode :character
Mean :40
3rd Qu.:49
Max. :91
NA's :1129
city state signs_of_mental_illness
Length:4087 Length:4087 Mode :logical
Class :character Class :character FALSE:2130
Mode :character Mode :character TRUE :875
NA's :1082
threat_level flee body_camera longitude
Length:4087 Length:4087 Mode :logical Min. :-158.02
Class :character Class :character FALSE:2682 1st Qu.:-107.59
Mode :character Mode :character TRUE :323 Median : -91.61
NA's :1082 Mean : -95.37
3rd Qu.: -82.73
Max. : -68.03
NA's :1252
latitude is_geocoding_exact year month
Min. :21.28 Mode :logical Min. :2015 Min. : 1.00
1st Qu.:33.77 FALSE:4 1st Qu.:2016 1st Qu.: 3.00
Median :36.87 TRUE :3001 Median :2018 Median : 6.00
Mean :37.39 NA's :1082 Mean :2018 Mean : 6.17
3rd Qu.:40.68 3rd Qu.:2019 3rd Qu.: 9.00
Max. :64.86 Max. :2021 Max. :12.00
NA's :1252 NA's :1082 NA's :1082
day
Min. : 1.00
1st Qu.: 8.00
Median :15.00
Mean :15.44
3rd Qu.:23.00
Max. :31.00
NA's :1082
summary(hisLats)
id name date manner_of_death
Min. : 5 Length:2169 Min. :2015-01-03 Length:2169
1st Qu.:1782 Class :character 1st Qu.:2016-08-08 Class :character
Median :3455 Mode :character Median :2018-02-25 Mode :character
Mean :3481 Mean :2018-03-07
3rd Qu.:5094 3rd Qu.:2019-09-18
Max. :7600 Max. :2021-12-26
NA's :1082 NA's :1082
armed age gender race
Length:2169 Min. :13.00 Length:2169 Length:2169
Class :character 1st Qu.:26.00 Class :character Class :character
Mode :character Median :33.00 Mode :character Mode :character
Mean :33.72
3rd Qu.:40.00
Max. :80.00
NA's :1111
city state signs_of_mental_illness
Length:2169 Length:2169 Mode :logical
Class :character Class :character FALSE:897
Mode :character Mode :character TRUE :190
NA's :1082
threat_level flee body_camera longitude
Length:2169 Length:2169 Mode :logical Min. :-157.88
Class :character Class :character FALSE:925 1st Qu.:-118.19
Mode :character Mode :character TRUE :162 Median :-111.00
NA's :1082 Mean :-106.26
3rd Qu.: -97.51
Max. : -70.79
NA's :1123
latitude is_geocoding_exact year month
Min. :21.32 Mode :logical Min. :2015 Min. : 1.000
1st Qu.:32.82 FALSE:2 1st Qu.:2016 1st Qu.: 3.000
Median :34.09 TRUE :1085 Median :2018 Median : 6.000
Mean :34.96 NA's :1082 Mean :2018 Mean : 6.159
3rd Qu.:37.61 3rd Qu.:2019 3rd Qu.: 9.000
Max. :48.75 Max. :2021 Max. :12.000
NA's :1123 NA's :1082 NA's :1082
day
Min. : 1.00
1st Qu.: 8.00
Median :16.00
Mean :15.74
3rd Qu.:23.00
Max. :31.00
NA's :1082
ggplot(fatal_police_shootings_data,aes(x=age)) +
geom_histogram(data=subset(fatal_police_shootings_data,race == 'B'),fill = "red", alpha = 0.2,binwidth = 1) +
geom_histogram(data=subset(fatal_police_shootings_data,race == 'W'),fill = "blue", alpha = 0.2,binwidth = 1) +
geom_histogram(data=subset(fatal_police_shootings_data,race == 'H'),fill = "yellow", alpha = 0.3,binwidth = 1)
Warning: Removed 31 rows containing non-finite values (stat_bin).
Warning: Removed 47 rows containing non-finite values (stat_bin).
Warning: Removed 29 rows containing non-finite values (stat_bin).
p1=ggplot(hisLats, aes(age))+
geom_histogram(color="black",fill="pink",binwidth=1, alpha=0.8)+
ggtitle("Individuals killed by age- Race/ethnicity: Hispanic/latino") + xlim(6, 87)
p2=ggplot(blacks, aes(age))+
geom_histogram(color="black",fill="green",binwidth=1, alpha=0.3)+
ggtitle("Individuals killed by age- Race/ethnicity: Black") + xlim(6, 87)
p3=ggplot(whites, aes(age))+
geom_histogram(color="black",fill="pink",binwidth=1, alpha=0.3)+
ggtitle("Individuals killed by age- Race/ethnicity: White") + xlim(6, 87)
g1 <- ggplotGrob(p1)
Warning: Removed 1111 rows containing non-finite values (stat_bin).
Warning: Removed 2 rows containing missing values (geom_bar).
g2 <- ggplotGrob(p2)
Warning: Removed 1114 rows containing non-finite values (stat_bin).
Warning: Removed 2 rows containing missing values (geom_bar).
g3 <- ggplotGrob(p3)
Warning: Removed 1131 rows containing non-finite values (stat_bin).
Warning: Removed 2 rows containing missing values (geom_bar).
g <- rbind(g1,g2, g3, size = "first")
g$widths <- unit.pmax(g1$widths,g2$widths, g3$widths)
grid.newpage()
grid.draw(g)
# Age break down with armed by region
ggplot(data=fatal_police_shootings_data,aes(x=fatal_police_shootings_data$region,y=age, fill=armed ))+
geom_boxplot(outlier.colour="Black", outlier.size=1, notch=FALSE)+
labs(x='Race/Ethnicity', y= 'Age')+
ggtitle("Age and 'armed' status of deceased by region")
Warning: Unknown or uninitialised column: `region`.
Warning: Unknown or uninitialised column: `region`.
Warning: Removed 364 rows containing non-finite values (stat_boxplot).
# Age break down with classification/cause of death by region
ggplot(data=fatal_police_shootings_data,aes(x=fatal_police_shootings_data$region,y=age, fill=manner_of_death ))+
geom_boxplot(outlier.colour="Black", outlier.size=1, notch=FALSE)+
labs(x='Race/Ethnicity', y= 'Age')+
ggtitle("Age and cause of death")
Warning: Unknown or uninitialised column: `region`.
Warning: Unknown or uninitialised column: `region`.
Warning: Removed 364 rows containing non-finite values (stat_boxplot).
# Age breakdown with race by region
ggplot(data=fatal_police_shootings_data,aes(x=fatal_police_shootings_data$region,y=age, fill=race))+
geom_boxplot(outlier.colour="Black", outlier.size=1, notch=FALSE)+
labs(x='Region', y= 'Age')
Warning: Unknown or uninitialised column: `region`.
Warning: Unknown or uninitialised column: `region`.
Warning: Removed 364 rows containing non-finite values (stat_boxplot).
knownraced %>% group_by(race) %>% dplyr::summarise(n = n())
ggplot(knownraced,aes(race)) +
geom_bar(fill="royalblue") +
ggtitle("Killings vs race/ethnicity of deceased")
library(tmap)
Warning: package ‘tmap’ was built under R version 4.1.2
library(tmaptools)
Warning: package ‘tmaptools’ was built under R version 4.1.2
shape <- read_sf("~/Assignment 1/USA_States_Generalized.shp")
{map_US + map_AL+ map_HI + tm_shape(shootings_sf)+tm_dots(size= 0.1, col="race", title= "Race", id="name", popup.vars = c("Age:" = "age", "Gender:" = "gender","Date Killed:"="date", "Armed:"="armed","Fleeing:"="flee", "Signs of Mental Health Issues:" = "signs_of_mental_illness","Manner of Death: " = "manner_of_death","State:" = "state"))+
tm_layout(title= "Map of Deadly Force US Police Shootings Jan 2015- December 2021",title.position = c('right', 'top'))+tmap_mode("view")}
tmap mode set to interactive viewing
legend.postion is used for plot mode. Use view.legend.position in tm_view to set the legend position in view mode.